package xyz.geminiwen.skinsprite.demo.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.global.configuration.Configuration;
import xyz.geminiwen.skinsprite.app.SkinnableAbility;
import xyz.geminiwen.skinsprite.app.SkinnableAbilitySlice;
import xyz.geminiwen.skinsprite.app.SkinnableComponentLayoutScatter;
import xyz.geminiwen.skinsprite.demo.ResourceTable;
import xyz.geminiwen.skinsprite.view.Skinnable;

public class MainAbilitySlice extends SkinnableAbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setReUIComponent(ResourceTable.Layout_ability_main);
        setSkinnableCallback(new SkinnableAbility.SkinnableCallback() {
            @Override
            public void beforeApplyDayNight() {
                new ToastDialog(MainAbilitySlice.this).setText("即将切换").show();
            }

            @Override
            public void onApplyDayNight() {
                new ToastDialog(MainAbilitySlice.this).setText("切换完成").show();
            }
        });

        findComponentById(ResourceTable.Id_btn_change).setClickedListener(component -> {
            int currentNightMode = wrapColorMode(getColorMode());
            switch (currentNightMode) {
                case Configuration.DARK_MODE: {
                    setDayNightMode(Configuration.LIGHT_MODE);
                    // Night mode is not active, we're in day time
                    break;
                }
                case Configuration.LIGHT_MODE: {
                    setDayNightMode(Configuration.DARK_MODE);
                    // Night mode is active, we're at night!
                    break;
                }
            }
        });

        Component component = SkinnableComponentLayoutScatter.getInstance(this).parse(ResourceTable.Layout_ability_main, null, false);
        if (component instanceof Skinnable) {
            new ToastDialog(MainAbilitySlice.this).setText("解析完成").show();
        }
    }

    private int wrapColorMode(int colorMode) {
        if (Configuration.AUTO_MODE == colorMode) {
            return getResourceManager().getConfiguration().getSystemColorMode();
        }
        return colorMode;
    }

    @Override
    protected int getDayNightStatusColor() {
        return getColor(ResourceTable.Color_statusColor);
    }
}
